<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\CaptchasRequest;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Http\Request;

class CaptchasController extends Controller
{
    //用户注册接口输入手机号,显示图片验证码
    public function store(CaptchasRequest $request)
    {
        $key='captchas_'.str_random(15);

        $phone=$request->phone;
        $builder=new CaptchaBuilder();
        $captch=$builder->build();
        $expired_at=now()->addMinutes(5);
        $result=[
            'captch_key'=>$key,
            'expired_at'=>$expired_at,
            'captcha_image'=>$captch->inline()
        ];
        \Cache::put($key,['phone'=>$request->phone,'code'=>$captch->getPhrase()],$expired_at);
        return  $this->response->array($result)->setStatusCode(201);
    }
}
